/*
 * Copyright (c) 2014 - 2017. Company.Domain Co.Ltd. All rights reserved.
 */

package cn.com.dhc.danlu.activity.delivery;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;

import java.util.ArrayList;

import cn.com.dhc.danlu.R;
import cn.com.dhc.danlu.activity.BaseActivity;
import cn.com.dhc.danlu.activity.goods.GoodInfoActivity;
import cn.com.dhc.danlu.activity.goods.GoodInfoInstructionActivity;
import cn.com.dhc.danlu.activity.goods.GoodInfoPromiseActivity;
import cn.com.dhc.danlu.dlware.OperatorUtils;
import cn.com.dhc.danlu.dlware.net.GeneralBusinessObserver;
import cn.com.dhc.danlu.util.CommonConst;
import cn.com.dhc.danlu.util.ToastUtil;
import cn.com.hd.mall.web.common.Tuple;
import cn.com.hd.mall.web.webservices.entity.request.order.OrderMerchSnapshotRequest;
import cn.com.hd.mall.web.webservices.entity.response.order.OrderMerchSnapshotResponse;
import cn.com.hd.mall.web.webservices.entity.response.order.OrderMerchSnapshotResponse.OrderMerchSnapshotModel;

import static cn.com.dhc.danlu.dlware.global.DanluApplication.agent;

/**
 * author: 刘立琦(<a href="mailto:liuliqi@danlu.com">liuliqi@danlu.com</a>)<br/>
 * version: 1.0.0<br/>
 * since: 2017-01-25 16:32<br/>
 * <p>
 * <p>
 * 商品快照页面
 * </p>
 */
public class DeliverLookForPhotoActivity extends BaseActivity implements OnClickListener {
    /**
     * 请求详细参数编码常量
     */
    private static final String REQUEST_ID = "merchandiseId";
    private static final String INTENT_DATA = "data";
    /**
     * 退出按钮
     */
    private LinearLayout llytBack;
    /**
     * 商品编号
     */
    private TextView tvShopNum;
    /**
     * 商品名称
     */
    private TextView tvShopName;
    /**
     * 商品图片
     */
    private SimpleDraweeView imgShopPhoto;
    /**
     * 购买价格
     */
    private TextView tvPrice;
    /**
     * 购买数量
     */
    private TextView tvNum;
    /**
     * 由谁负责配送
     */
    private TextView tvSaleSend;
    /**
     * 查看商品最新消息
     */
    private TextView tvLookShopDetail;
    /**
     * 商品描述
     */
    private RelativeLayout rlytShopDescribe;
    /**
     * 单露保证
     */
    private RelativeLayout rlytPromise;
    /**
     * 规格容器
     */
    private RelativeLayout rlytShopGuige;
    /**
     * 规格箭头
     */
    private ImageView imgBtnGuige;
    /**
     * 小订单号
     */
    private String merchId;
    /**
     * 规格信息
     */
    private TextView tvGuige;
    /**
     * 规格容器
     */
    private LinearLayout llytShopGuige;

    private ArrayList<String> urls;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_deliver_look_for_photo);
        initViews();
        initData();
    }

    @Override
    protected String getActivityTitle() {
        return null;
    }

    private void initData() {
        // 获取订单号
        merchId = getIntent().getStringExtra(CommonConst.MyOrderConst.SMALL_ORDER_SHOP_NUM);
        String orderNo = getIntent().getStringExtra(CommonConst.MyOrderConst.SMALL_ORDER_P_KEY);
        // 设置请求对象s
        OrderMerchSnapshotRequest request = new OrderMerchSnapshotRequest();
        // 设置token
        request.setToken(agent.getToken());
        // 设置订单好
        request.setOrderNo(orderNo);
        // 设置id
        request.setMerchId(merchId);
        // 请求
        agent.getApi()
                .getSnapShot(request)
                .compose(OperatorUtils.<OrderMerchSnapshotResponse>defaultSchedulers())
                .subscribe(new GeneralBusinessObserver<OrderMerchSnapshotResponse>() {
                    @Override
                    protected void onSuccess(OrderMerchSnapshotResponse model) {
                        setData(model.getMerchSnapshot()); // 设置信息
                    }

                    @Override
                    protected void onAlertTips(String displayMessage) {
                        ToastUtil.getInstance().show(displayMessage);
                    }
                });
    }

    /**
     * 设置信息
     */
    @SuppressLint("SetTextI18n")
    private void setData(OrderMerchSnapshotModel model) {
        // 设置监听
        setListener();
        if (model != null) {
            // 查看详情
            if (model.getCanShowMerchDetail().equals(CommonConst.SnapShotConst.ORDER_DETAIL_0)) {
                tvLookShopDetail.setText(getString(R.string.look_for_shop_photo_cannot_look));
            } else {
                tvLookShopDetail.setText(getString(R.string.look_for_shop_photo_look_detail));
                tvLookShopDetail.setOnClickListener(this);
            }
            // 商品编号
            tvShopNum.setText(model.getMerchNo());
            // 商品名
            tvShopName.setText(model.getMerchName());
            // 价格
            tvPrice.setText(getResources().getString(R.string.rmb_unit_label) + model.getUnitPrice());
            // 数量
            tvNum.setText(model.getMerchCount() + model.getMerchSpec());
            // 配送商
            tvSaleSend.setText(model.getSellerName());
            // 显示规格
            llytShopGuige.setVisibility(View.VISIBLE);
            // 规格信息
            tvGuige.setText(getPropertyDesc(model));
            // 设置图片
            imgShopPhoto.setImageURI(model.getPicUrl());

            urls = new ArrayList<>();
            urls.addAll(model.getAlbumPicUrl());
        }
    }

    private void setListener() {
        llytBack.setOnClickListener(this);
        rlytPromise.setOnClickListener(this);
        rlytShopGuige.setOnClickListener(this);
        rlytShopDescribe.setOnClickListener(this);
    }

    /**
     * 获取规格信息
     */
    private String getPropertyDesc(OrderMerchSnapshotModel response) {
        StringBuilder sb = new StringBuilder();
        for (Tuple<String, String> tuple : response.getPropertyList()) {
            sb.append(tuple.getFirstValue());
            sb.append(getString(R.string.char_sign));
            sb.append(tuple.getSecondValue());
            sb.append("\n");
        }
        return sb.toString();
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()) {
            // 退出
            case R.id.llytBack:
                finish();
                break;

            // 查看详情
            case R.id.tvLookShopDetail:
                intent.setClass(DeliverLookForPhotoActivity.this, GoodInfoActivity.class);
                // 设置跳转参数
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                intent.putExtra(REQUEST_ID, merchId);
                startActivity(intent);
                break;

            // 查看规格
            case R.id.rlytShopGuige:
                if (llytShopGuige.getVisibility() == View.VISIBLE) {
                    llytShopGuige.setVisibility(View.GONE);
                    imgBtnGuige.setBackgroundResource(R.drawable.img_top_btn);
                } else {
                    llytShopGuige.setVisibility(View.VISIBLE);
                    imgBtnGuige.setBackgroundResource(R.drawable.img_bottom_btn);
                }
                break;

            // 商品描述
            case R.id.rlytShopDescribe:
                intent.setClass(DeliverLookForPhotoActivity.this, GoodInfoInstructionActivity.class);

                // 将商品详细模型对象保存到传值对象中
                if (urls != null) {
                    intent.putStringArrayListExtra(INTENT_DATA, urls);
                }

                // 设置跳转参数
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                // 跳转
                startActivity(intent);
                break;

            // 丹露保证
            case R.id.rlytPromise:
                intent.setClass(DeliverLookForPhotoActivity.this, GoodInfoPromiseActivity.class);
                // 设置跳转参数
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                // 跳转
                startActivity(intent);
                break;

            default:
                break;
        }
    }

    private void initViews() {
        llytBack = (LinearLayout) findViewById(R.id.llytBack);
        tvShopNum = (TextView) findViewById(R.id.tvShopNum);
        tvShopName = (TextView) findViewById(R.id.tvShopName);
        imgShopPhoto = (SimpleDraweeView) findViewById(R.id.imgShopPhoto);
        tvPrice = (TextView) findViewById(R.id.tvPrice);
        tvNum = (TextView) findViewById(R.id.tvNum);
        tvSaleSend = (TextView) findViewById(R.id.tvSaleSend);
        tvLookShopDetail = (TextView) findViewById(R.id.tvLookShopDetail);
        rlytShopDescribe = (RelativeLayout) findViewById(R.id.rlytShopDescribe);
        rlytPromise = (RelativeLayout) findViewById(R.id.rlytPromise);
        rlytShopGuige = (RelativeLayout) findViewById(R.id.rlytShopGuige);
        imgBtnGuige = (ImageView) findViewById(R.id.imgBtnGuige);
        tvGuige = (TextView) findViewById(R.id.tvGuige);
        llytShopGuige = (LinearLayout) findViewById(R.id.llytShopGuige);
    }
}
